package com.czh.framework.security.authorization;

import com.czh.framework.security.web.UserDetailsEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Component("ex")
public class CustomAuthorization {
    public boolean author(String ...auth) {
        UserDetailsEntity principal = (UserDetailsEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<GrantedAuthority> authorities = (List<GrantedAuthority>) principal.getAuthorities();
        for (GrantedAuthority authority : authorities) {
            if(Arrays.asList(auth).contains(authority.getAuthority())) {
                return true;
            }
        }
        return false;
    }
}
